package cn.edu.gxnu.dsa.lab;

import java.util.Scanner;

import cn.edu.gxnu.dsa.sort.IntType;
import cn.edu.gxnu.dsa.sort.SqListWithSort;

public class Lab7 {

	public static void main(String[] args) {
		int opCode = 7;
		int item;
		int index;
		int inputItem;
		int indexFound;
		SqListWithSort sequcenceList = null;
		Scanner sc = new Scanner(System.in);

		while (opCode != 0) {
			welcomMsg();
			opCode = sc.nextInt();
			switch (opCode) {
			case 1:
				if (sequcenceList == null) {
					System.out.println("Please input the max size of the sequence list:");
					int maxSize = sc.nextInt();
					System.out.println("Please input how many items that you want to put in the list:");
					int listSize = sc.nextInt();
					sequcenceList = new SqListWithSort(maxSize);
					for (int i = 0; i < listSize; i++) {
						System.out.println("Please input No." + (i + 1) + " item:");
						item = sc.nextInt();
						try {
							sequcenceList.insert(i, new IntType(item));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					sequcenceList.display();
				} else {
					System.out.println("The sequence is exist! Please do other operations.");
				}
				break;
			case 2:
				if (sequcenceList == null) {
					System.out.println("!!!Please create your sequence List first!!!");
					break;
				}
				System.out.println("Please input the insert position:");
				index = sc.nextInt();
				System.out.println("Please input the item:");
				inputItem = sc.nextInt();
				try {
					sequcenceList.insert(index - 1, inputItem);
				} catch (Exception e) {
					e.printStackTrace();
				}
				sequcenceList.display();
				break;
			case 3:
				if (sequcenceList == null) {
					System.out.println("!!!Please create your sequence List first!!!");
					break;
				}
				System.out.println("Please input the delete position:");
				index = sc.nextInt();
				try {
					sequcenceList.remove(index);
				} catch (Exception e) {
					e.printStackTrace();
				}
				sequcenceList.display();
				break;
			case 4:
				if (sequcenceList == null) {
					System.out.println("!!!Please create your sequence List first!!!");
					break;
				}
				System.out.println("Plese input the item you want to search:");
				inputItem = sc.nextInt();
				indexFound = sequcenceList.indexOf(inputItem);
				if (indexFound == -1) {
					System.out.println("There is no item found of " + "'" + inputItem + "'");
				} else {
					System.out.println("The No." + indexFound + " item is " + "'" + inputItem + "'");
				}
				break;
			case 5:
				sequcenceList.display();
				sequcenceList.sortWithGuard();
				System.out.println("Sorting finished!");
				sequcenceList.display();
				break;
			case 6:
				opCode = 0;
			default:
				break;
			}
		}
		sc.close();

	}

	static void welcomMsg() {
		System.out.println("+----------------------------------------+");
		System.out.println("|Lab: A demo for sorting a sequence list |");
		System.out.println("|Author: TANG Zhiyong  Version: 0.1      |");
		System.out.println("|Eclipse Public License - v 1.0          |");
		System.out.println("|Guangxi Normal University               |");
		System.out.println("|                                     	 |");
		System.out.println("|Please select the operation:            |");
		System.out.println("|1 create a new sequence list            |");
		System.out.println("|2 insert an item                        |");
		System.out.println("|3 delete an item                        |");
		System.out.println("|4 find an item                          |");
		System.out.println("|5 sort the list                         |");
		System.out.println("|6 close this demo                       |");
		System.out.println("+----------------------------------------+");
	}

}
